package com.naiterui.ehp.bs.im.network.mqtt;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.naiterui.ehp.bp.utils.context.SpringContextHolder;
import com.naiterui.ehp.bs.im.network.mqtt.pool.MqttClientManager;
import com.naiterui.ehp.bs.im.network.mqtt.pool.MqttConnection;

/**
 * 基于自定义MQTT客户端连接池的操作封装
 */
public class MqttClientUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(MqttClientUtils.class);

    /**
     * mqtt连接池管理对象
     */
    private static MqttClientManager mqttClientManager = SpringContextHolder.getBean("mqttClientManager", MqttClientManager.class);

    /**
     * mqtt发送消息的编码格式
     */
    private static final String CHARSET = "utf-8";

    /**
     * 通讯质量
     */
    private static final int QOS = 2;

    /**
     * 使用默认topic推送message(与clientId一致)
     */
    public static void publishMessageToClient(String clientId, String message) {
        LOGGER.info("publishMessageToClient INFO ; clientId={}, message={}", clientId, message);
        MqttConnection connection = null;
        try {
            connection = mqttClientManager.getConnection(clientId);
            LOGGER.info("publishMessageToClient INFO ; clientId={},targetUrl={}", clientId, connection.getMqttClient().getServerURI());
            connection.publish(clientId, message, CHARSET, QOS);
        } catch (Exception e) {
            LOGGER.error("publishMessageToClient ERROR ; clientId={},message={}", clientId, message, e, e);
        } finally {
            if (null != connection) {
                connection.close();
            }
        }
    }

    /**
     * 推送到客户端对应的topic
     */
    public static void publishMessageToTopic(String clientId, String topic, String message) {
        LOGGER.info("publishMessageToTopic INFO ; clientId={},topic={}, message={}", clientId, topic, message);
        MqttConnection connection = null;
        try {
            connection = mqttClientManager.getConnection(clientId);
            LOGGER.info("publishMessageToClient INFO ; clientId={},topic={},targetUrl={}", clientId, topic,
                    connection.getMqttClient().getServerURI());
            connection.publish(topic, message, CHARSET, QOS);
        } catch (Exception e) {
            LOGGER.error("publishMessageToTopic ERROR ; clientId={},topic={},message={}", clientId, topic, message, e, e);
        } finally {
            if (null != connection) {
                connection.close();
            }
        }
    }

    /**
     * 推送到所有服务器
     */
    public static void pushToAllServers(String topic, String message) {
        LOGGER.info("pushToAllServers INFO ; topic={}, message={}", topic, message);

        for (String server : mqttClientManager.getServers()) {
            MqttConnection connection = null;
            try {
                connection = mqttClientManager.getNodeConnection(server);
                connection.publish(topic, message, CHARSET, QOS);
            } catch (Exception e) {
                LOGGER.error("pushToAllServers ERROR ; server={},topic={}, message={}", server, topic, message, e, e);
            } finally {
                if (null != connection) {
                    connection.close();
                }
            }
        }
    }

    public static MqttClientManager getMqttClientManager() {
        return mqttClientManager;
    }

    public static void setMqttClientManager(MqttClientManager mqttClientManager) {
        MqttClientUtils.mqttClientManager = mqttClientManager;
    }

}
